/*
 * Copyright (c) 2010 CCX(China) Co.,Ltd. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * CCX(China) Co.,Ltd. ("Confidential Information").
 * It may not be copied or reproduced in any manner without the express 
 * written permission of CCX(China) Co.,Ltd.
 */

package cn.com.ccxe.web;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.ccxe.service.LoginBussiness;

import com.ccxe.manage.filters.MdPwd;
import com.ccxe.manage.service.CurrUserInfo;
import com.ccxe.manage.service.impl.MenuProcess;

/**
 * 
 * @author hetao
 * @version
 */
public class LoginAction extends BasciAction {

	private static final long 		serialVersionUID = 3L;
	private static final Logger 	logger = LoggerFactory.getLogger(LoginAction.class);

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void dispatch(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// setContext(resp);
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		String loginType = String.valueOf(req.getParameter("loginType"));
		CurrUserInfo userInfo = (CurrUserInfo) req.getSession().getAttribute("currUser");
		String adminName = (String) req.getSession().getAttribute("userName");
		if (isQuit(req, resp)) {
			return;
		}
		if (userInfo != null || adminName != null) {
			forward(req, resp, "/login.jsp");
			return;
		}

		if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
			error(req, resp, "用户名密码不能为空！");
			return;
		}
		password = MdPwd.getMD5(password);

		// 分发登陆处理，现有三种角色登陆：
		// 1、权限系统管理员
		// 2、监控系统管理员
		// 3、客户
		if (StringUtils.isEmpty(loginType)) {
			loginType = loginType.trim();
		}
		if ("permissionor".equals(loginType)) {
			systemAdminLogin(req, resp, username, password);
		} else if ("monitor".equals(loginType)) {
			webmasterLogin(req, resp, username, password);
		} else {
			customerLogin(req, resp, username, password);
		}
	}

	private void customerLogin(HttpServletRequest req, HttpServletResponse resp, String username, String password)
			throws IOException, ServletException {
		LoginBussiness loginBussiness = new LoginBussiness();
		boolean isLogin = loginBussiness.customerLogin(username, password);
		if (!isLogin) {
			error(req, resp, "用户名密码有误！");
			return;
		}
		logger.info("用户 " + username + " 登陆!");
		CurrUserInfo cui = loginBussiness.getCurrUserInfo(username);
		req.getSession().setAttribute("currUser", cui);
		resp.sendRedirect("/zebra/customer/index.jsp");
		// req.getRequestDispatcher("/zebra/customer/index.jsp?username=&password=").forward(req,
		// resp);
	}

	private void webmasterLogin(HttpServletRequest req, HttpServletResponse resp, String username, String password)
			throws ServletException, IOException {
		LoginBussiness loginBussiness = new LoginBussiness();
		boolean isLogin = loginBussiness.customerLogin(username, password);
		if (!isLogin) {
			error(req, resp, "用户名密码有误！");
			return;
		}
		CurrUserInfo cui = loginBussiness.getCurrUserInfo(username);
		if (!("M".equalsIgnoreCase(cui.getUserType()))) {
			customerLogin(req, resp, username, password);
			return;
		}
		logger.info("监控系统管理员管理员 " + username + " 登陆!");
		req.getSession().setAttribute("currUser", cui);
		resp.sendRedirect("/zebra/monitor/index.jsp");
		// req.getRequestDispatcher("/zebra/monitor/index.jsp?username=&password=").forward(req,
		// resp);
	}

	private void systemAdminLogin(HttpServletRequest req, HttpServletResponse resp, String username, String password)
			throws ServletException, IOException {
		LoginBussiness loginBussiness = new LoginBussiness();
		boolean isLogin = loginBussiness.adminLogin(username, password);
		if (!isLogin) {
			error(req, resp, "用户名密码有误！");
			return;
		}
		logger.info("权限系统管理员管理员  " + username + " 登陆!");
		req.getSession().setAttribute("userName", username);
		req.getSession().setAttribute("rootMenu", new MenuProcess().getRootMenu());
		resp.sendRedirect("/jsp/index.jsp");
		// req.getRequestDispatcher("/jsp/index.jsp?username=&password=").forward(req,
		// resp);
	}

	/**
	 * 
	 * @param req
	 * @param resp
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	protected boolean isQuit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		if (req.getParameter("quit") != null) {
			logger.info(" 在 " + new Date(System.currentTimeMillis()) + " 退出了");
			req.getSession().invalidate();
			req.getRequestDispatcher("/login.jsp").forward(req, resp);
			return true;
		}
		return false;
	}

}
